-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't leak the function that is called on drop #111875
Conversation
r? @TaKO8Ki (rustbot has picked a reviewer for you, use r? to override) |
r? @Nilstrieb |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not because it's a good idea but out of curiosity, what unsafe approach did you imagine?
@@ -102,21 +102,27 @@ pub mod unord; | |||
pub use ena::undo_log; | |||
pub use ena::unify; | |||
|
|||
pub struct OnDrop<F: Fn()>(pub F); | |||
/// Returns a structure that calls `f` when dropped. | |||
pub fn defer<F: FnOnce()>(f: F) -> OnDrop<F> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on the rename
this touches ty::tls and while I'm fairly certain that it won't affect perf let's just make sure it doesn't |
This comment has been minimized.
This comment has been minimized.
⌛ Trying commit e2b9530 with merge 0a7230578c08af7d4c955133f51b1eb698f1a6b2... |
Basically using pub fn defer<F: FnOnce()>(f: F) -> OnDrop<F> {
OnDrop(ManuallyDrop::new(f))
}
pub struct OnDrop<F: FnOnce()>(ManuallyDrop<F>);
impl<F: FnOnce()> OnDrop<F> {
pub fn disable(self) {
// Prevent `self`'s drop from running and taking/calling `self.0`
let mut this = ManuallyDrop::new(self);
// Safety:
// `self.0` is only taken on drop and we prevented it from running.
unsafe { ManuallyDrop::drop(&mut this.0) };
}
}
impl<F: FnOnce()> Drop for OnDrop<F> {
fn drop(&mut self) {
// Safety:
// This is the only place that takes `self.0` (except `disable` which is
// mutually exclusive with this function);
let f = unsafe { ManuallyDrop::take(&mut self.0) };
f();
}
} |
ah, since |
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (0a7230578c08af7d4c955133f51b1eb698f1a6b2): comparison URL. Overall result: ❌ regressions - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 645.989s -> 647.159s (0.18%) |
@bors r+ rollup |
…iaskrgr Rollup of 4 pull requests Successful merges: - rust-lang#95198 (Add slice::{split_,}{first,last}_chunk{,_mut}) - rust-lang#109899 (Use apple-m1 as target CPU for aarch64-apple-darwin.) - rust-lang#111624 (Emit diagnostic for privately uninhabited uncovered witnesses.) - rust-lang#111875 (Don't leak the function that is called on drop) r? `@ghost` `@rustbot` modify labels: rollup
It probably wasn't causing problems anyway, but still, a
// this leaks, please don't pass anything that owns memory
is not sustainable.I could implement a version which does not require
Option
, but it would requireunsafe
, at which point it's probably not worth it.